using System;
using System.Collections.Generic;
using System.Text;
using Barrage.GUI.Meshes;
using SlimDX;
using SlimDX.Direct3D9;
using Barrage;
using Spacedash.Logic;
using Barrage.Textures;
using Barrage.Meshes;

namespace Spacedash.Game.Items
{
    /// <summary>
    /// A Speedometer to show the player its ship current speed
    /// </summary>
    public class GUISpeedometer : Item
    {
        private const float BASE_UV = 64 / 768.0f;
        private const int NUM_SPOTS = 14;
        private const int LEVEL1_SPOTS = 10;
        private const float LEVEL_UV = 24 / 768.0f;
        private const float SPOT_UV = 48 / 768.0f;
        /// <summary>
        /// Item Width
        /// </summary>
        public const float SIZE_X = 64;
        /// <summary>
        /// Item height
        /// </summary>
        public const float SIZE_Y = 740;
        /// <summary>
        /// Associated ship
        /// </summary>
        public Ship ship;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="pos">Item position</param>
        /// <param name="ship">Associated ship</param>
        public GUISpeedometer(Vector2 pos,Ship ship)            
        {
            this.ship = ship;
            motion.Translation = new Vector3(pos, 0);
            addMesh(new ProgressiveRectangle(TextureManager.Textures["GUI/Speedometer_FR.png"], new Vector2(SIZE_X, SIZE_Y), ProgressiveRectangleLayout.VerticalDownToUp)); 
            addMesh(new SimpleRectangle(TextureManager.Textures["GUI/Speedometer_BG.png"], new Vector2(SIZE_X, SIZE_Y)));
            
        }
        
        ProgressiveRectangle Rectangle { get { return (ProgressiveRectangle)meshes[0]; } }

        /// <summary>
        /// \internal Updates speedometer data
        /// </summary>
        protected override void DoUpdate()
        {
            float speedPercent = ship.Speed / ship.MaxSpeed;
            float shipProgress = Math.Min(1,speedPercent)*0.66f+Math.Max(0,(speedPercent-1)/ship.ProfileBoost)*0.36f;            

            int nSpots = (int)(shipProgress * NUM_SPOTS);
            Rectangle.Progress = BASE_UV + nSpots * SPOT_UV + ((shipProgress * NUM_SPOTS>LEVEL1_SPOTS)?LEVEL_UV:0);

            base.DoUpdate();
        }

    }
}